<?php
/**
 * $Id: emails_controller.php 71 2008-12-25 07:28:33Z gpo_gigapromoters $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 71 $
 * @lastModifiedBy		$LastChangedBy: gpo_gigapromoters $
 * @lastChanged			$Date: 2008-12-25 12:58:33 +0530 (Thu, 25 Dec 2008) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Incomplete class which will allow admin to edit email templates as per their needs.
 * Needs big refactoring.
 * If anyone wants to finish this class. Please discuss with me.
 *
 */
class EmailsController extends CmsAppController
{
	var $name = 'Emails';
	var $uses = array();
	//var $templates = array();

	var $components = array('Email', 'Session');

	var $helpers = array('Html', 'Form', 'Javascript');

	/*
	var $adminLinks = array('/cms/emails/edit' => 'Manage Emails');
	var $adminOrder = 11;
	*/

	function _getTemplates()
	{
		//concatinate the path with ~ 
		$templates = array(APP.'plugins'.DS.'users'.DS.'views'.DS.'elements'.DS.'email'.DS.'~'.DS.'register'.'.ctp'=>'Register',
		APP.'plugins'.DS.'users'.DS.'views'.DS.'elements'.DS.'email'.DS.'~'.DS.'forgot_password'.'.ctp'=>'Forgot Password',
		APP.'plugins'.DS.'invites'.DS.'views'.DS.'elements'.DS.'email'.DS.'~'.DS.'send_to_friend'.'.ctp'=>'Send to friend');
		return $templates;	
	}
	
	/**
	 * This function enables easy editing of emails
	 *
	 */
	function admin_edit()
	{
		$templates = $this->_getTemplates();
		$this->set('templates', $templates);
		if($this->data)
		{
			$template = explode('~', $this->data['Email']['template']);		
			$html_template = $template[0].'html'.$template[1];
			$text_template =  $template[0].'text'.$template[1];
			if(isset($template))
			{
				if(isset($this->data['Email']['body_html']) || isset($this->data['Email']['body_text']))
				{
					$this->createMailTemplate($html_template, $this->data['Email']['body_html']);
					$this->createMailTemplate($text_template, $this->data['Email']['body_text']);	
					$this->Session->setFlash("Email template is modified successfully.");
					$this->redirect(array('action'=>'admin_edit'));
				}
				$this->set('body_html', $this->getMailTemplate($html_template));
				$this->set('body_text', $this->getMailTemplate($text_template));
				$this->set('template',  $this->data['Email']['template']);
			}
		}
	}

	function getMailTemplate($path)
	{
		$filename = $path;
		$fp = fopen($filename, 'r');
		$contents = @fread($fp, filesize($filename));
		fclose($fp);
		return $contents;
	}

	function createMailTemplate($template, $body)
	{
		$fp = fopen($template, 'w');
		fwrite($fp, $body);
		fclose($fp);
	}

}

?>